package com.lzc.tlmail.tlmailproduct.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.lzc.tlmail.tlmailproduct.dao.BrandDao;
import com.lzc.tlmail.tlmailproduct.dao.CategoryDao;
import com.lzc.tlmail.tlmailproduct.entity.AttrAttrgroupRelationEntity;
import com.lzc.tlmail.tlmailproduct.entity.BrandEntity;
import com.lzc.tlmail.tlmailproduct.entity.CategoryEntity;
import com.lzc.tlmail.tlmailproduct.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzc.common.utils.PageUtils;
import com.lzc.common.utils.Query;

import com.lzc.tlmail.tlmailproduct.dao.CategoryBrandRelationDao;
import com.lzc.tlmail.tlmailproduct.entity.CategoryBrandRelationEntity;
import com.lzc.tlmail.tlmailproduct.service.CategoryBrandRelationService;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {
    @Resource
    private BrandDao brandDao;

    @Resource
    private CategoryDao categoryDao;

    @Resource
    private BrandService brandService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 保存品牌跟分类关联
     * @param brandId
     * @param catelogId
     */
    @Override
    public void saveDetail(Long brandId, Long catelogId) {
        BrandEntity brandEntity = brandDao.selectById(brandId);
        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
        categoryBrandRelationEntity.setBrandId(brandId);
        categoryBrandRelationEntity.setCatelogId(catelogId);
        categoryBrandRelationEntity.setBrandName(brandEntity.getName());
        categoryBrandRelationEntity.setCatelogName(categoryEntity.getName());
        this.getBaseMapper().insert(categoryBrandRelationEntity);
    }

    @Override
    public void updateCategory(Long catId, String name) {
        //修改分类关联
        CategoryBrandRelationEntity categoryBrand = new CategoryBrandRelationEntity();
        categoryBrand.setCatelogId(catId);
        categoryBrand.setCatelogName(name);
        this.getBaseMapper().update(categoryBrand,new UpdateWrapper<CategoryBrandRelationEntity>()
            .eq("catelog_id",categoryBrand.getCatelogId())
        );
    }

    @Override
    public void updateBrand(Long brandId, String name) {
        //修改品牌关联
        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
        categoryBrandRelationEntity.setBrandId(brandId);
        categoryBrandRelationEntity.setBrandName(name);
        this.getBaseMapper().update(categoryBrandRelationEntity,new UpdateWrapper<CategoryBrandRelationEntity>()
        .eq("brand_id",brandId)
        );
    }

    /**
     * 获取分类关联的所有品牌
     * @param catId
     * @return
     */
    @Override
    public List<BrandEntity> getBrandsByCatId(Long catId) {
        QueryWrapper<CategoryBrandRelationEntity> queryWrapper = new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId);
        List<CategoryBrandRelationEntity> categoryBrandRelationEntities = this.getBaseMapper().selectList(queryWrapper);
        List<BrandEntity> brandEntities = categoryBrandRelationEntities.stream().map(categoryBrandRelationEntity -> {
            BrandEntity brandEntity = brandService.getById(categoryBrandRelationEntity.getBrandId());
            return brandEntity;
        }).collect(Collectors.toList());
        return brandEntities;
    }

}